package configs

import (
	"bytes"
	_ "embed"
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/tools/gins"
	gorm_v2 "gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/tools/gormv2"
	"gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/tools/zaps"
	"github.com/spf13/viper"
)

//go:embed config.yaml
var f []byte

type Configuration struct {
	Zap   zaps.ZapConfig      `mapstructure:"zap"`
	Gin   gins.GinConfig      `mapstructure:"gin"`
	Gorm2 gorm_v2.Gorm2Config `mapstructure:"gorm2"`
}

func loadDefault() *viper.Viper {
	//embed 方式 ,作为默认配置项
	vp := viper.New()
	//开始读取环境变量，如果没有调用这个函数，则下面无法读取到path的值
	vp.AutomaticEnv()
	vp.SetConfigType("yaml")
	err := vp.ReadConfig(bytes.NewReader(f))
	if nil != err {
		panic(fmt.Sprintf("loadDefault err:%v", err))
	}
	vp.WatchConfig()

	return vp
}

func Load() *viper.Viper {
	return loadDefault()
}
